package com.hx.haowumall.haowumallorder.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.hx.haowumall.haowumallorder.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "9021000136609266";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCjmWBanQCb75nAv7yt3ou2wycAG0c/h38/mbNkxfTDIdzZa39izyjP7nkkoGF8Nd77IdvDGT6HFTbZHKYcDFuvA0liEuNw598ZvtPGTglPBLjHtvPIyJU94osFA8k+RPxKLLyNBvygMoS3ZRXuvQhfVFxwIQ0HRWo0gSQuQAE6q+woLv5Py3xdQ/sOINCwyla6GE8XcOS3JUKIcJ7SnUqB1NmbU8bJu+BqDVPs8HEK5oFJXpyLk0Cgzz4QSWsg0QgCNSBMMratqMS5FBWYLRd+iUU6LaCinQ3Zf/fMDD4DVJ6ST7BBpy3I0GJ9znU2sfTJ4XoWeztbHkA1ohcmICFbAgMBAAECggEASBw321juxs0CsXISIXVBkgpM64AalIcN8KO37ghPTZ9kcDUwzgXP0G8l2J4UuiIoqfsPmdxLslEIfwRgt2Y/F1JUizUWKVMbBIcXY4ZMFhdXtzgOFodLOR7c9hXoI1lW9v7nESZuPdJmq0uUMkoCIbrWj9oRmw3etUvi/bvajtg4Hb7Xg0ZXQQhx+EyBzxZp93rJ3sDTHqh8IUo8uQajUyjocwQ7Qpb92CHtLyItWcDyVjmoJLNgFdjtyYwYMkKhBsVcJ/LpzdnuYfAzXydB26KpOVNTn/5BslNE8yJCg0hrPm7fmSKMYFcmOdm/11h3mR4NUe5Z9TTERhVHkeKpeQKBgQDwVGRDKWKEO11BevmrX/etfLKOWr2lS6viZeWL5RoEryNhz1pymGpiKCJOQ1apNFnvyZgsGqZ/WB33+p7QywvfeZlmL3FAuEPeyAP0Vra0oZ9d0dsSKHYABjigAg/TvN6nYeYs1dCNhHsta+gSv0k0lyslVxhefP4PA6UAJ6KHjQKBgQCuRDDGq3VTxbPUaV0iAT96gsW1c2nF5e8zAtKOQCzohkFiI2/f/oAvEIOHCj9tUi6O5qfRPSXR8Vjprxjpc/A3gc7BdxPqhisEhGG2OYsCG3d+yA4Y3Xrvy5h0mJ9Ev4tl/BAF/uCZqmbtMHTs+L19p/hSx8XM6TJtGHzHPhC+hwKBgHfggIqHsB5pRiENaFA1dGPTGXWsiYBT+I7baN6ZDptbiPiW53TV1erUPhhvumB4Pow/MGp/eKF4oKo7FtDrFycHjp7L91/jCeesOVy9jDEZlVbFANNT31gvwjr+DEr/oMg5KwkeNgMBHDgR5RbE140mAl2DziIDku6GUN1857NZAoGAbKVw73vYMzlQK37hJKE4jU3Yq47gBBSds+TvluZl2TZSrze+uRz3l+Ao0FlQP/z7r/YX7Hi+Coz4zQBwfKPewQx59d4imZW6Xh+Wt8BabR7oui9JsrDqw3Pggdjhe0AcTaH/72z/ZG57qycJzvApW50MUbfhFybQXHnm2WVVlSMCgYBpwfqyueFiAgUjTNgkxSphaKzwEvwVtkgW39WWf+a2ylSC4/NAdZNhNjIHDYMa0mD0ldjqAohsxmQccCLgqMEcTDXEQGkbkj0ySndJsIECEYpp929ZIwUvTBn57XIJW050tFr7hq8JEsX0HL+ol19Yh6mHA0u2KlCN3GmXvihadw==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhSCGZJ6DkP1EVVBmiIwsPXj2lbdX8zJnlDcr2Je8u21timp6Mnf3c0JUgccU4smfODu3nnSog9Pmjd7Ts9BOH58xtvVc9iNinq0EJjGW2Dzv7Uw4dZUuflSziRrWs1HtPcufkuKpMu2GA82w+y/6kbY7ia2Z57ddFBlyEwSD1i/6amZufV6LzKLqXXX7Q+H2mlP8DHWR9S9Q6Mx+xnqhPZXlUM1KfqTv5ESYvEzMKIQuTPplCHLEHxoS20clkHcTTyv0WdDZLHph9vydBYBhvcy/l7fDp8XVdXLfA/JQiDPZTX5Ph7RU7zkyU4g8mZdoLj3Z9GtVl6/yQEk82uX4aQIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://haowushop.nat300.top/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.haowumall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "10m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
